const copyTxt = function (text, fn) {
    // 复制功能
    if (typeof document.execCommand !== 'function') {
        console.log('复制失败，请长按复制')
        return
    }
    var dom = document.createElement('textarea')
    dom.value = text
    dom.setAttribute('style', 'display: block;width: 1px;height: 1px;')
    document.body.appendChild(dom)
    dom.select()
    var result = document.execCommand('copy')
    document.body.removeChild(dom)
    if (result) {
        console.log('复制成功')
        typeof fn === 'function' && fn()
        return
    }
    if (typeof document.createRange !== 'function') {
        console.log('复制失败，请长按复制')
        return
    }
    var range = document.createRange()
    var div = document.createElement('div')
    div.innerHTML = text
    div.setAttribute('style', 'height: 1px;fontSize: 1px;overflow: hidden;')
    document.body.appendChild(div)
    range.selectNode(div)
    var selection = window.getSelection()
    console.log(selection)
    if (selection.rangeCount > 0) {
        selection.removeAllRanges()
    }
    selection.addRange(range)
    document.execCommand('copy')
    typeof fn === 'function' && fn()
    console.log('复制成功')
}

/* 
method 2:
export function copyToBoard(value) {
    const element = document.createElement('textarea')
    document.body.appendChild(element)
    element.value = value
    element.select()
    if (document.execCommand('copy')) {
        document.execCommand('copy')
        document.body.removeChild(element)
        return true
    }
    document.body.removeChild(element)
    return false
} */

/* 如果复制成功返回true
copyToBoard('xxxx') */
